package controllers

import (
	"fmt"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"

	"demo0606/modules"
)

type Events struct{}

func (ev Events) AddEvents(c *gin.Context) {
	data := &modules.Events{}
	c.BindJSON(&data)
	timeNow := time.Now().Unix()
	data.AddTime = int(timeNow)
	err := modules.DB.Create(&data).Error
	if err != nil {
		c.JSON(500, gin.H{
			"success": false,
			"message": "新增事件轴失败！",
		})
	} else {
		c.JSON(200, gin.H{
			"success": true,
			"message": "新增事件轴成功！",
		})
	}

}

func (ev Events) GetEvents(c *gin.Context) {
	eventsList := []modules.Events{}
	err := modules.DB.Find(&eventsList).Error
	if err != nil {
		c.JSON(500, gin.H{
			"success": false,
			"message": "获取事件轴失败！",
		})
	} else {
		c.JSON(200, gin.H{
			"success": true,
			"message": "获取事件轴成功！",
			"data":    eventsList,
		})
	}

}
func (ev Events) GetEventsById(c *gin.Context) {
	id := c.Query("id")
	idnum, _ := strconv.Atoi(id)
	events := modules.Events{
		Id: idnum,
	}
	err := modules.DB.Find(&events).Error
	if err != nil {
		c.JSON(500, gin.H{
			"success": false,
			"message": "获取事件轴失败！",
		})
	} else {
		c.JSON(200, gin.H{
			"success": true,
			"message": "获取事件轴成功！",
			"data":    events,
		})
	}

}
func (ev Events) DeleteEvents(c *gin.Context) {
	id := c.Query("id")
	// events:=modules.Events{
	// 	Id:id,
	// }
	// err:=modules.DB.Delete(&events).Error
	err := modules.DB.Delete(&modules.Events{}, id).Error
	if err != nil {
		c.JSON(500, gin.H{
			"success": false,
			"message": "删除事件轴失败！",
		})
	} else {
		c.JSON(200, gin.H{
			"success": true,
			"message": "删除事件轴成功！",
		})
	}

}

func (ev Events) EditEvents(c *gin.Context) {
	editData := &modules.Events{}
	c.BindJSON(&editData)
	println("editData", editData)
	fmt.Printf("hahahahah%v", editData)
	err := modules.DB.Updates(&editData).Error
	if err != nil {
		c.JSON(500, gin.H{
			"success": false,
			"message": "修改事件轴失败！",
		})
	} else {
		c.JSON(200, gin.H{
			"success": true,
			"message": "修改事件轴成功！",
		})
	}

}
